package gameobjects;

import gamesound.*;

import java.util.ArrayList;
import java.util.List;

import javagame.Nivo;

import org.newdawn.slick.*;

public class Igrac {
	
	public static final Color LIME = new Color (181, 230, 29);
	public static final Color DARK_RED = new Color(136, 0, 21);
	public static final Color GOLD = new Color(255, 201, 14);
	public static final Color ORANGE = new Color(255, 106, 34);
	public static final Color ROSE = new Color(255, 174, 201);
	public static final Color PURPLE = new Color(163, 73, 164);
	
	private static Igrac instance = null;
	private static int startHP = 20;
	
	public enum pravac_kretanja {GORE, DOLE, LEVO, DESNO};
	
	private int[] duration = {200, 200};
	private int[] duration2 = {2000, 2000};
	
	private Image HP_bar, S_bar, rocket;
	private Animation tenk;
	private Animation movingUp;
	private Animation movingDown;
	private Animation movingLeft; 
	private Animation movingRight;
	private Animation eksplozija;
	
	private int tenk_HP; // helth tenka
	private boolean shield_flag; // oznaka da li ima shield
	private boolean ziv; // da li je ziv
	private boolean raketa;
	private boolean presao;
	private boolean presaoBonus;
	private int raketaVreme;
	private ArrayList<Metak> metak;
	private ArrayList<Metak> rakete;
	
	private static final float startX = 461;
	private static final float startY = 718;
	private static final float verticalX = 974;
	private static final float verticalY = 666;
	private static final float verticalBonusX = 974;
	private static final float verticalBonusY = 332;
	
	
	private float tenkX;
	private float tenkY;
	
	private boolean men;//da li je muski ili zenski model
	private boolean zvezda;//da li je pokupio zvezda power up
	
	private int bodovi;
	
	private Igrac () {
		metak = new ArrayList<Metak>();
		rakete = new ArrayList<Metak>();
		tenk_HP = startHP;
		shield_flag = false;
		ziv = true;
		tenkX = startX;
		tenkY = startY;
		raketa = false;
		raketaVreme = 0;
		zvezda = false;
		presao = false;
		presaoBonus = false;
		bodovi = 0;
	}
	
	public synchronized static Igrac dohvIgraca() {
		if(instance == null) {
			instance = new Igrac();
		}
		return instance;
	}
	
	public void setAnimation() {
		try {
			if(men == true) {
				if (zvezda == false) {
					Image [] walkUp = {new Image("res/tenk_gore1.png"), new Image("res/tenk_gore2.png")};
					Image [] walkDown = {new Image("res/tenk_dole1.png"), new Image("res/tenk_dole2.png")};
					Image [] walkLeft = {new Image("res/tenk_levo1.png"), new Image("res/tenk_levo2.png")};
					Image [] walkRight = {new Image("res/tenk_desno1.png"), new Image("res/tenk_desno2.png")};
					this.setMovingUp(new Animation(walkUp, this.getDuration(), true));//true se stavlja zbog autoupdate
					this.setMovingDown(new Animation(walkDown, this.getDuration(), true));
					this.setMovingLeft(new Animation(walkLeft, this.getDuration(), true));
					this.setMovingRight(new Animation(walkRight, this.getDuration(), true));
				}
				else {
					Image [] walkUp = {new Image("res/tenk_upgrade_gore1.png"), new Image("res/tenk_upgrade_gore2.png")};
					Image [] walkDown = {new Image("res/tenk_upgrade_dole1.png"), new Image("res/tenk_upgrade_dole2.png")};
					Image [] walkLeft = {new Image("res/tenk_upgrade_levo1.png"), new Image("res/tenk_upgrade_levo2.png")};
					Image [] walkRight = {new Image("res/tenk_upgrade_desno1.png"), new Image("res/tenk_upgrade_desno2.png")};
					this.setMovingUp(new Animation(walkUp, this.getDuration(), true));//true se stavlja zbog autoupdate
					this.setMovingDown(new Animation(walkDown, this.getDuration(), true));
					this.setMovingLeft(new Animation(walkLeft, this.getDuration(), true));
					this.setMovingRight(new Animation(walkRight, this.getDuration(), true));
				}
			}
			else {
				if (zvezda == false) {
					Image [] walkUp = {new Image("res/tenkz_gore1.png"), new Image("res/tenkz_gore2.png")};
					Image [] walkDown = {new Image("res/tenkz_dole1.png"), new Image("res/tenkz_dole2.png")};
					Image [] walkLeft = {new Image("res/tenkz_levo1.png"), new Image("res/tenkz_levo2.png")};
					Image [] walkRight = {new Image("res/tenkz_desno1.png"), new Image("res/tenkz_desno2.png")};
					this.setMovingUp(new Animation(walkUp, this.getDuration(), true));//true se stavlja zbog autoupdate
					this.setMovingDown(new Animation(walkDown, this.getDuration(), true));
					this.setMovingLeft(new Animation(walkLeft, this.getDuration(), true));
					this.setMovingRight(new Animation(walkRight, this.getDuration(), true));
				}
				else {
					Image [] walkUp = {new Image("res/tenkz_upgrade_gore1.png"), new Image("res/tenkz_upgrade_gore2.png")};
					Image [] walkDown = {new Image("res/tenkz_upgrade_dole1.png"), new Image("res/tenkz_upgrade_dole2.png")};
					Image [] walkLeft = {new Image("res/tenkz_upgrade_levo1.png"), new Image("res/tenkz_upgrade_levo2.png")};
					Image [] walkRight = {new Image("res/tenkz_upgrade_desno1.png"), new Image("res/tenkz_upgrade_desno2.png")};
					this.setMovingUp(new Animation(walkUp, this.getDuration(), true));//true se stavlja zbog autoupdate
					this.setMovingDown(new Animation(walkDown, this.getDuration(), true));
					this.setMovingLeft(new Animation(walkLeft, this.getDuration(), true));
					this.setMovingRight(new Animation(walkRight, this.getDuration(), true));
				}
			}
			Image [] eksp = {new Image("res/bum.png"), new Image("res/bum2.png")};
			this.setEksplozija(new Animation(eksp, this.getDuration2(), true));
			rocket = new Image("res/rocket.png");
			S_bar = new Image ("res/stit_krug.png");
		} catch (SlickException e) {}
		//if (tenk.equals(null)) this.setTenk(this.getMovingUp());
	}
	
	public void postaviPol(boolean men) throws SlickException {
		this.men = men;
		setAnimation();
	}
	
	public int getTenk_HP() {
		return tenk_HP;
	}
	public void setTenk_HP(int tenkHP) {
		tenk_HP = tenkHP;
	}
	public boolean isShield_flag() {
		return shield_flag;
	}
	public void setShield_flag(boolean shieldFlag) {
		shield_flag = shieldFlag;
	}
	public boolean isRaketa() {
		return raketa;
	}
	public void setRaketa(boolean raketa) {
		this.raketa = raketa;
	}
	public List<Metak> getMetak() {
		return metak;
	}
	public void setMetak(ArrayList<Metak> metak) {
		this.metak = metak;
	}
	public ArrayList<Metak> getRakete() {
		return rakete;
	}
	public void setRakete(ArrayList<Metak> rakete) {
		this.rakete = rakete;
	}
	public float getStartX() {
		return startX;
	}
	public float getStartY() {
		return startY;
	}
	public float getTenkX() {
		return tenkX;
	}
	public void setTenkX(float tenkX) {
		this.tenkX = tenkX;
	}
	public float getTenkY() {
		return tenkY;
	}
	public void setTenkY(float tenkY) {
		this.tenkY = tenkY;
	}

	public boolean isZiv() {
		return ziv;
	}

	public void setZiv(boolean ziv) {
		this.ziv = ziv;
	}
	
	public Image getHP_bar() {
		return HP_bar;
	}

	public void setHP_bar(Image hPBar) {
		HP_bar = hPBar;
	}

	public Image getS_bar() {
		return S_bar;
	}

	public void setS_bar(Image sBar) {
		S_bar = sBar;
	}

	public Animation getTenk() {
		return tenk;
	}

	public void setTenk(Animation tenk) {
		this.tenk = tenk;
	}

	public Animation getMovingUp() {
		return movingUp;
	}

	public void setMovingUp(Animation movingUp) {
		this.movingUp = movingUp;
	}

	public Animation getMovingDown() {
		return movingDown;
	}

	public void setMovingDown(Animation movingDown) {
		this.movingDown = movingDown;
	}

	public Animation getMovingLeft() {
		return movingLeft;
	}

	public void setMovingLeft(Animation movingLeft) {
		this.movingLeft = movingLeft;
	}

	public Animation getMovingRight() {
		return movingRight;
	}

	public void setMovingRight(Animation movingRight) {
		this.movingRight = movingRight;
	}
	
	public Animation getEksplozija() {
		return eksplozija;
	}

	public void setEksplozija(Animation eksplozija) {
		this.eksplozija = eksplozija;
	}

	public int[] getDuration() {
		return duration;
	}

	public void setDuration(int[] duration) {
		this.duration = duration;
	}
	
	public void setDuration2(int[] duration2) {
		this.duration2 = duration2;
	}

	public int[] getDuration2() {
		return duration2;
	}

	public boolean isMen() {
		return men;
	}

	public void setMen(boolean men) {
		this.men = men;
	}
	
	public void eksplodiraj () {
		eksplozija.draw(tenkX, tenkY);
		eksplozija.setLooping(false);
	}
	
	public void crtaj(Graphics g) throws SlickException {
		Color stara = g.getColor();
		if (ziv == true) {
			tenk.draw(tenkX, tenkY);
		
			if(shield_flag == true ){
				S_bar.draw(tenkX - 18, tenkY - 18);
			}
			else {
				g.setColor(DARK_RED);
				g.fillRect(tenkX, tenkY-20, 50, 10);
				g.setColor(LIME);
				if (tenk_HP <= 20)
					g.fillRect(tenkX, tenkY-20, 50*tenk_HP/startHP, 10);
				else 
					g.fillRect(tenkX, tenkY-20, 50*tenk_HP/5000, 10);
				g.setColor(Color.black);
				g.drawRect(tenkX, tenkY-20, 50, 10);
			}
		}
		g.setColor(Color.black);
		g.fillRect(3, 3, 114, 22);
		g.setColor(Color.white);
		g.drawString("Score: " + bodovi, 5, 5);
		if (raketa) {
			if (raketaVreme==0) {
				rocket = new Image("res/rocket.png");
			}
			else {
				rocket = new Image("res/rocket2.png");
			}
			rocket.draw(5, 712);
		}
		g.setColor(stara);
	}
	
	public void pucaj (boolean belo) throws SlickException {
		if(zvezda) {
			Metak m1 = new Metak();
			Metak m2 = new Metak();
			if (men) {
				if (belo) {
					m1.setBoja(GOLD);
					m2.setBoja(GOLD);
				}
				else {
					m1.setBoja(ORANGE);
					m2.setBoja(ORANGE);
				}
			}
			else {
				if (belo) {
					m1.setBoja(ROSE);
					m2.setBoja(ROSE);
				}
				else {
					m1.setBoja(PURPLE);
					m2.setBoja(PURPLE);
				}
			}
			if (tenk.equals(movingLeft)) {
				m1.setMetakX(tenkX - 5);
				m1.setMetakY(tenkY + 18);
				m1.setSmer(3);
				m2.setMetakX(tenkX - 5);
				m2.setMetakY(tenkY + 26);
				m2.setSmer(3);
			}
			if (tenk.equals(movingRight)){
				m1.setMetakX(tenkX + 50);
				m1.setMetakY(tenkY + 18);
				m1.setSmer(2);
				m2.setMetakX(tenkX + 50);
				m2.setMetakY(tenkY + 26);
				m2.setSmer(2);
			}
			if (tenk.equals(movingUp)){
				m1.setMetakX(tenkX + 18);
				m1.setMetakY(tenkY - 5);
				m1.setSmer(1);
				m2.setMetakX(tenkX + 26);
				m2.setMetakY(tenkY - 5);
				m2.setSmer(1);
			}
			if (tenk.equals(movingDown)){
				m1.setMetakX(tenkX + 18);
				m1.setMetakY(tenkY + 50);
				m1.setSmer(0);
				m2.setMetakX(tenkX + 26);
				m2.setMetakY(tenkY + 50);
				m2.setSmer(0);
			}
			metak.add(m1);
			metak.add(m2);
		}
		else {
			Metak m = new Metak();
			if(men) {
				if (belo) m.setBoja(GOLD);
				else m.setBoja(ORANGE);
			}
			else {
				if (belo) m.setBoja(ROSE);
				else m.setBoja(PURPLE);
			}
			if (tenk.equals(movingLeft)) {
				m.setMetakX(tenkX - 5);
				m.setMetakY(tenkY + 22);
				m.setSmer(3);
			}
			if (tenk.equals(movingRight)){
				m.setMetakX(tenkX + 50);
				m.setMetakY(tenkY + 22);
				m.setSmer(2);
			}
			if (tenk.equals(movingUp)){
				m.setMetakX(tenkX + 22);
				m.setMetakY(tenkY - 5);
				m.setSmer(1);
			}
			if (tenk.equals(movingDown)){
				m.setMetakX(tenkX + 22);
				m.setMetakY(tenkY + 50);
				m.setSmer(0);
			}
			metak.add(m);
		}
	}
	
	public void pucajRaketu () throws SlickException {
		if (raketaVreme == 0) {
			Zvuk.dohvZvuk().pucaj();
			Metak r = new Metak();
			if (tenk.equals(movingLeft)) {
				r.setMetak(new Image("res/raketa_levo.png"));
				r.setMetakX(tenkX - 30);
				r.setMetakY(tenkY + 18.5f);
				r.setSmer(3);
			}
			if (tenk.equals(movingRight)){
				r.setMetak(new Image("res/raketa_desno.png"));
				r.setMetakX(tenkX + 50);
				r.setMetakY(tenkY + 18.5f);
				r.setSmer(2);
			}
			if (tenk.equals(movingUp)){
				r.setMetak(new Image("res/raketa_gore.png"));
				r.setMetakX(tenkX + 18.5f);
				r.setMetakY(tenkY - 30);
				r.setSmer(1);
			}
			if (tenk.equals(movingDown)){
				r.setMetak(new Image("res/raketa_dole.png"));
				r.setMetakX(tenkX + 18.5f);
				r.setMetakY(tenkY + 50);
				r.setSmer(0);
			}
			r.setRaketa(true);
			rakete.add(r);
			raketaVreme = 400;
		}
	}
	
	public void setAutoUpdate (boolean stanje) {
		tenk.setAutoUpdate(stanje);
	}
	
	public void ozivi() {
		tenk_HP = startHP;
		shield_flag = false;
		raketa = false;
		raketaVreme = 0;
		ziv = true;
		zvezda = false;
		if (!Nivo.isVerticalLevel()) {
			tenkX = startX;
			tenkY = startY;
			setTenk(getMovingUp());
		}
		else {
			tenkX = verticalX;
			tenkY = verticalY;
			setTenk(getMovingLeft());
		}
		bodovi = 0;
		presao = false;
		presaoBonus = false;
		metak.clear();
		rakete.clear();
		eksplozija.setLooping(true);
		eksplozija.restart();
	}
	
	public void noviLevel(boolean dvaIgraca) {
		if (tenkY <= 0) {
			tenkY = startY;
			if(dvaIgraca)
				tenkX = startX;
		}
		else if (tenkX <= 0)
			tenkX = 974;
		else if (tenkX >= 974)
			tenkX = 0;
		presao = false;
		presaoBonus = false;
		metak.clear();
		rakete.clear();
	}
	
	public void noviLevelBonus(boolean dvaIgraca) {
		if (tenkX <= 0) {
			tenkX = verticalBonusX;
			tenkY = verticalBonusY;
		}
		else if (tenkX >= 974) {
			tenkX = 0;
			tenkY = verticalBonusY;
		}
		presao = false;
		presaoBonus = false;
		metak.clear();
		rakete.clear();
	}

	public void odbrojavaj () {
		if (raketaVreme > 0)
			raketaVreme--;
	}

	public boolean isZvezda() {
		return zvezda;
	}

	public void setZvezda(boolean zvezda) {
		this.zvezda = zvezda;
	}

	public int getBodovi() {
		return bodovi;
	}

	public void setBodovi(int bodovi) {
		this.bodovi += bodovi;
	}

	public void setPresao(boolean presao) {
		this.presao = presao;
	}

	public boolean isPresao() {
		return presao;
	}

	public void setPresaoBonus(boolean presaoBonus) {
		this.presaoBonus = presaoBonus;
	}

	public boolean isPresaoBonus() {
		return presaoBonus;
	}

}
